//@login & register
const express = require("express");
const router = express.Router();
const db = require("../../db/index")
const passport = require("passport")


// $router POST api/profile/add
// @desc 添加profile信息接口
// @access private
router.post("/add", passport.authenticate("jwt", { session: false }), (req, res) => {
  let Datestr = "";
  let data = new Date();
  Datestr = `${data.getFullYear()}-${data.getMonth() + 1
    }-${data.getDate()}`;
  const { type, describes, icome, expend, remark, crash } = req.body
  const sqladd = `INSERT INTO profile(type,describes,incode,expend,remark,date,crash) VALUES ('${type}','${describes}','${icome}','${expend}','${remark}','${Datestr}','${crash}')`
  db.query(sqladd, (err, result) => {
    if (err) {
      return res.send({
        code: 0,
        msg: err.message
      })
    }
    if (result.affectedRows != 1) {
      return res.send({
        code: 0,
        msg: "添加失败"
      })
    } else {
      return res.send({
        code: 1,
        msg: "添加成功"
      })
    }
  })
})



// $router get api/profile/query
// @desc 查询profile数据
// @access private
router.get("/query", (req, res) => {
  const sqlcc = `SELECT * FROM profile`
  db.query(sqlcc, (err, result) => {
    if (err) {
      return res.send({
        code: 0,
        msg: err.message
      })
    }
    if (result.length == 0) {
      res.send({
        code: 0,
        msg: "查询失败"
      })
    } else {
      res.send({
        code: 1,
        msg: "查询成功",
        data: result
      })
    }

  })
})


// $router get api/profile/query/:id
// @desc 获取单个信息
// @access private
router.get("/query/:id", (req, res) => {
  const { id } = req.params

  const sqlcc = `SELECT * FROM profile WHERE id = ${id}`
  db.query(sqlcc, (err, result) => {
    if (err) {
      return res.send({
        code: 0,
        msg: err.message
      })
    }
    if (result.length == 0) {
      res.send({
        code: 0,
        msg: "查询失败"
      })
    } else {
      res.send({
        code: 1,
        msg: "查询成功",
        data: result
      })
    }

  })
})


// $router POST api/profile/edit/:id
// @desc 编辑profile信息接口
// @access private
router.post("/edit/:id", passport.authenticate("jwt", { session: false }), (req, res) => {
  const id = req.params.id
  const { type, describes, icome, expend, remark, crash } = req.body
  const sqlupda = `UPDATE profile SET type = '${type}', describes = '${describes}', incode = '${icome}', expend = '${expend}',remark = '${remark}', crash = '${crash}' WHERE id = ${id};`
  db.query(sqlupda, (err, result) => {
    console.log(result);
    if (err) {
      return res.send({
        code: 0,
        msg: err.message
      })
    }
    if (result.affectedRows != 1) {
      return res.send({
        code: 0,
        msg: "修改失败"
      })
    } else {
      return res.send({
        code: 1,
        msg: "修改成功"
      })
    }
  })
})
// $router delete api/profile/delete/:id
// @desc 删除profile信息接口
// @access private
router.delete("/delete/:id", passport.authenticate("jwt", { session: false }), (req, res) => {
  const id = req.params.id
  const sqldel = `DELETE FROM profile WHERE id = ${id}`
  db.query(sqldel, (err, result) => {
    console.log(result);
    if (err) {
      return res.send({
        code: 0,
        msg: err.message
      })
    }
    if (result.affectedRows != 1) {
      return res.send({
        code: 0,
        msg: "删除失败"
      })
    } else {
      return res.send({
        code: 1,
        msg: "删除成功"
      })
    }
  })
})


module.exports = router